(define-module (mach vm-allocate)
  #:use-module (mach ffi)
  #:use-module (mach types)
  #:use-module ((mach task) #:select (task-self))
  #:export (vm-allocate vm-deallocate))

(define-ffi ("vm_allocate" %vm-allocate)
  (err identity) ; FIXME ???
  (target-task ffi:pointer %unwrap-task)
  (address ffi:pointer make-pointer)
  (size vm-size identity)
  ;; FIXME really a bool FIXME what's this?
  (anywhere ffi:int bool->ffi-int))

(define* (vm-allocate address size anywhere #:optional (task (task-self)))
  "Allocate a region of virtual memory in @var{task}, starting at @var{address}
and of length @var{size}.
By default, @var{task} is ourself.  ADDRESS must be passed as an integer."
  (%vm-allocate task address size anywhere))

(define-ffi ("vm_deallocate" %vm-deallocate)
  (err identity)
  (task ffi:pointer %unwrap-vm-task)
  (address ffi:pointer make-pointer)
  (size vm-size identity))

(define* (vm-deallocate address size #:optional (task (task-self)))
  "Relinquishes access to a region of the address space of @var{task},
starting at @var{address} (an integer) and of length @var{size},
causing further access to that memory to fail.
By default, @var{task} is ourself."
  (%vm-deallocate task address size))
